package com.tencent.sr.iris.basic.mp.dependency;


import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.interfaces.client.task.IrisGroupTaskClient;
import com.tencent.sr.iris.activity.interfaces.request.task.GroupTaskListNewRequest;
import com.tencent.sr.iris.activity.interfaces.request.task.MarketTaskCalenderRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.task.GroupTaskListResponse;
import com.tencent.sr.rmall.common.exception.code.BaseErrorCode;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author nixiaolin
 * @date 2022/11/7 14:37
 */
@Slf4j
@Component
public class GroupTaskDependency {

    @Resource
    IrisGroupTaskClient groupTaskClient;

    /**
     * 分页查询团长任务列表--进行中
     *
     * @return
     */
    public PaginationResponse<GroupTaskListResponse> listInProgress(
            PaginationRequest<GroupTaskListNewRequest> request) {

        HttpResult<PaginationResponse<GroupTaskListResponse>> result = groupTaskClient.listInProgress(
                request);
        if (result == null || !result.isSuccess()) {
            log.error("listInProgress fail, request:{},response:{}", JSON.toJSONString(request),
                    JSON.toJSONString(result));
        }
        log.info("listInProgress success, request:{},response:{}", JSON.toJSONString(request),
                JSON.toJSONString(result));
        return result.getData();
    }

    /**
     * 分页查询团长任务列表--已结束
     *
     * @return
     */
    public PaginationResponse<GroupTaskListResponse> listEnded(PaginationRequest<GroupTaskListNewRequest> request) {
        HttpResult<PaginationResponse<GroupTaskListResponse>> result = groupTaskClient.listEnded(
                request);
        if (result == null || !result.isSuccess()) {
            log.error("listEnded fail, request:{},response:{}", JSON.toJSONString(request), JSON.toJSONString(result));
        }
        log.info("listEnded success, request:{},response:{}", JSON.toJSONString(request), JSON.toJSONString(result));
        return result.getData();
    }

    public List<GroupTaskListResponse> marketCalendarList(MarketTaskCalenderRequest request) {
        HttpResult<List<GroupTaskListResponse>> result = null;
        try {
            result = groupTaskClient.marketCalendarList(request);
            if (result != null && result.isSuccess()) {
                log.info("marketCalendarList successRequest:{} response = {}",
                        JsonUtils.toJsonString(request), result);
                return result.getData();
            }
        } catch (Exception e) {
            log.error("marketCalendarList failRequest:{} result: {}",
                    JsonUtils.toJsonString(request), result, e);
            throw new TRetailBizException(e);
        }
        log.error("marketCalendarList failRequest:{} result:{}",
                JsonUtils.toJsonString(request), result);
        if (Objects.isNull(result)) {
            throw new TRetailBizException(BaseErrorCode.REMOTE_CALL_LOST, "marketCalendarList 返回为空");
        }
        throw new TRetailBizException(result.getCode(), result.getMsg());

    }
}
